package com.njitzx.controller;


import com.njitzx.annotation.Log;
import com.njitzx.en.BusinessType;
import com.njitzx.entity.dto.PassWordVO;
import com.njitzx.entity.dto.UpdateUserVO;
import com.njitzx.entity.po.User;
import com.njitzx.entity.vo.UserInfoVO;
import com.njitzx.entity.vo.UserVO;
import com.njitzx.service.UserService;
import com.github.pagehelper.PageInfo;
import com.njitzx.util.BaseContext;
import com.njitzx.util.PwdUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.njitzx.entity.vo.Result;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Map;

import lombok.RequiredArgsConstructor;


import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 周鑫
 * @since 2024-08-20
 */
@Api(tags = "用户表接口")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @ApiOperation("用户表分页查询")
    @GetMapping
    public Result findPage(@ApiParam("页码") @RequestParam(defaultValue = "1", required = true) Integer pageNum,
                           @ApiParam("页大小") @RequestParam(defaultValue = "5", required = true) Integer pageSize,
                           @ApiParam("查询条件") UserVO user) {
        Map<String, Object> pageInfo = userService.findPage(pageNum, pageSize, user);
        return Result.success(pageInfo);
    }

    @ApiOperation("根据主键查询用户表")
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        return Result.success(userService.findById(id));
    }

    @ApiOperation("新增用户表")
    @PostMapping
    public Result add(@RequestBody User user) {
        userService.add(user);
        return Result.success();
    }

    @ApiOperation("修改用户表")
    @PutMapping("/{id}")
    public Result modify(@PathVariable Integer id, @RequestBody User user) {
        user.setId(id);
        userService.modify(user);
        return Result.success();
    }

    @ApiOperation("根据主键删除用户表")
    @DeleteMapping("/{id}")
    public Result removeById(@PathVariable Serializable id) {
        userService.removeById(id);
        return Result.success();
    }

    @Log(title = "修改用户状态", businessType = BusinessType.UPDATE)
    @PutMapping("/state/{id}/{state}")
    public Result updateStatus(@PathVariable(required = true, value = "id") Integer id, @PathVariable(required = true, value = "state") Integer state) {
        userService.updateStatus(id, state);
        return Result.success();
    }

    @Log(title = "修改用户权限", businessType = BusinessType.UPDATE)
    @PutMapping("/role/{id}/{roleId}")
    public Result updateRoleId(@PathVariable(required = true, value = "id") Integer id, @PathVariable(required = true, value = "roleId") Integer roleId) {
        userService.updateRoleId(id, roleId);
        return Result.success();
    }

    @ApiOperation("查询用户信息")
    @GetMapping("/user/info")
    public Result getInfo() {
        UserInfoVO userInfoVO = userService.getUserInfo();
//        userInfoVO.setPassword(PwdUtil);
        return Result.success(userInfoVO);
    }

    @ApiOperation("修改用户密码")
    @PostMapping("/modifyPassword")
    public Result modifyPassword(String password) {
        User user = new User();
        user.setId(BaseContext.getCurrentId());
        user.setPassword(PwdUtil.encrypt(password));
        userService.modify(user);
        return Result.success();
    }

    @ApiOperation("更新用户信息")
    @PutMapping("/updateInfo")
    public Result Update(@RequestBody UpdateUserVO updateUserVO) {
        Integer id = BaseContext.getCurrentId();
        userService.updateInfo(id,updateUserVO);
        return Result.success("更新成功");
    }

    //修改用户头像


    @ApiOperation("修改用户密码")
    @PostMapping("/changePassword")
    public Result changePassWord(@RequestBody PassWordVO passWordVO) {
        Integer id = BaseContext.getCurrentId();
        userService.changePassword(id, passWordVO);
        return Result.success("修改成功");
    }
}
